<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form size="mini" :model="dataForm" :rules="dataRule" ref="dataForm" label-width="100px">
      <el-form-item label="任务名称" prop="jobName">
        <el-input clearable v-model="dataForm.jobName" placeholder="任务名称"></el-input>
      </el-form-item>
      <el-form-item label="bean名称" prop="beanName">
        <el-input clearable v-model="dataForm.beanName" placeholder="spring bean名称, 如: commonService"></el-input>
      </el-form-item>
      <el-form-item label="方法名称" prop="methodName">
        <el-input clearable v-model="dataForm.methodName" placeholder="方法名称"></el-input>
      </el-form-item>
      <el-form-item label="参数" prop="params">
        <el-input clearable v-model="dataForm.params" placeholder="参数"></el-input>
      </el-form-item>
      <el-form-item label="cron表达式" prop="cronExpression">
        <el-input clearable v-model="dataForm.cronExpression" placeholder="如: 0 0 12 * * ?"></el-input>
      </el-form-item>
      <el-form-item label="cron说明" prop="cronDesc">
        <el-input clearable v-model="dataForm.cronDesc" placeholder="cron说明"></el-input>
      </el-form-item>
      <el-row type="flex" class="row-bg">
        <el-col :span="12">
          <el-form-item label="排序" prop="orderNum">
            <el-input clearable v-model="dataForm.orderNum" placeholder="排序"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="备注" prop="remark">
            <el-input clearable v-model="dataForm.remark" placeholder="备注"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button size="mini" @click="visible = false">取消</el-button>
      <el-button size="mini" type="primary" @click="dataFormSubmit()" :loading="btnLoading">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        btnLoading: false,
        dataForm: {
          id: 0,
          jobName: '',
          beanName: 'statQueryService',
          methodName: 'exchangeData',
          params: '',
          cronExpression: '0 0 12 * * ?',
          cronDesc: '',
          remark: '',
          orderNum: '',
          status: 0
        },
        dataRule: {
          jobName: [
            { required: true, message: '任务名称不能为空', trigger: 'blur' }
          ],
          beanName: [
            { required: true, message: '用户名不能为空', trigger: 'blur' }
          ],
          methodName: [
            { required: true, message: '方法名称不能为空', trigger: 'blur' }
          ],
          cronExpression: [
            { required: true, message: 'cron表达式不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.requestUrl(`/job/schedule/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.requestParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.jobName = data.schedule.jobName
                this.dataForm.beanName = data.schedule.beanName
                this.dataForm.methodName = data.schedule.methodName
                this.dataForm.params = data.schedule.params
                this.dataForm.cronExpression = data.schedule.cronExpression
                this.dataForm.cronDesc = data.schedule.cronDesc
                this.dataForm.remark = data.schedule.remark
                this.dataForm.status = data.schedule.status
                this.dataForm.orderNum = data.schedule.orderNum
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.btnLoading = true
            this.$http({
              url: this.$http.requestUrl(`/job/schedule/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.requestData({
                'jobId': this.dataForm.id || undefined,
                'jobName': this.dataForm.jobName,
                'beanName': this.dataForm.beanName,
                'methodName': this.dataForm.methodName,
                'params': this.dataForm.params,
                'cronExpression': this.dataForm.cronExpression,
                'cronDesc': this.dataForm.cronDesc,
                'remark': this.dataForm.remark,
                'orderNum': this.dataForm.orderNum,
                'status': !this.dataForm.id ? undefined : this.dataForm.status
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
              this.btnLoading = false
            })
          }
        })
      }
    }
  }
</script>
